//
//  FXEmoticonToolBar.swift
//  微博(Swift)
//
//  Created by V on 16/1/13.
//  Copyright © 2016年 V. All rights reserved.
//

import UIKit


// 定义一个枚举
enum FXEmoticonType: Int {
    case Recent = 1000
    case Default = 1001
    case Emoji = 1002
    case Lxh = 1003
}

class FXEmoticonToolBar: UIStackView {
    
    // MARK: - 属性
    var currentSelectedButton: UIButton?
    // 定义一个闭包，当点击 toolBar 的时候，将点击事件传递给 Keyboard 
    var childButtonClickClose: ((type: FXEmoticonType) -> ())?
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    // MARK: - 设置UI
    private func setupUI(){
        // 设置子控件的分布方式
        distribution = .FillEqually
        addChildButton("最近", bgImageNamed: "compose_emotion_table_left", type: .Recent)
        addChildButton("默认", bgImageNamed: "compose_emotion_table_mid", type: .Default)
        addChildButton("Emoji", bgImageNamed: "compose_emotion_table_mid", type: .Emoji)
        addChildButton("浪小花", bgImageNamed: "compose_emotion_table_right", type: .Lxh)
        
        
        // 接受通知 ，当 cell 滚动到相对应的 section 的时候就让相应的button成为选中状态
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "emoticonButtonSelectedWithSection:", name: FXEmoticonButtonSelectedNotification, object: nil)
    }
    
    // 添加按钮的操作
    private func addChildButton(title: String, bgImageNamed: String, type: FXEmoticonType){
        let button = UIButton()
        // 添加按钮点击
        button.addTarget(self, action: "childButtonClick:", forControlEvents: .TouchUpInside)
        // 用 tag 记录 button 的类型
        button.tag = type.rawValue
        if button.tag == 1001 {
            button.selected = true
            currentSelectedButton = button
        }
        // 1.字体大小 2.文本 3.背景图
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        button.setTitle(title, forState: .Normal)
        button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.grayColor(), forState: UIControlState.Selected)
        // 设置不同状态的背景图
        button.setBackgroundImage(UIImage(named: "\(bgImageNamed)_normal"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "\(bgImageNamed)_selected"), forState: UIControlState.Selected)
        
        // 使用 addSubview(button) 不能够显示出来
        addArrangedSubview(button)
    }
    
    
    
    // MARK: - 监听 表情键盘的 toolBar 的按钮点击
    // 把点击 button 事件传递给 键盘View。让键盘View 实现切换相应的表情组
    @objc private func childButtonClick(button: UIButton){
        // 1. 把点击的按钮状态变为 select 2. 把点击事件回调给键盘
        // 如果点击的和上次点击的按钮是一个按钮就什么都不做
        if currentSelectedButton == button {
            return
        }
        currentSelectedButton?.selected = false
        button.selected = true
        currentSelectedButton = button
        childButtonClickClose!(type: FXEmoticonType(rawValue: button.tag)!)
    }
    
    
    
    // 当 cell 滚动到相对应的 section 的时候就让相应的button成为选中状态
    @objc private func emoticonButtonSelectedWithSection(notify: NSNotification) {
        // 1. 取出需要设为选中状态的button，记录并设置
        let indexPath = notify.object as! NSIndexPath
        // print(section)
        let button = viewWithTag(1000 + indexPath.section) as! UIButton
        if currentSelectedButton == button {
            return
        }
        button.selected = true
        currentSelectedButton?.selected = false
        currentSelectedButton = button
    }

    
    
}
